package com.twu.biblioteca;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by aswathik on 6/20/14.
 */
public class LoginValidator implements MenuItem {

    private UserInterface userInterface;
    private String menuString;
    LoginValidator(UserInterface userInterface) {
        this.userInterface = userInterface;
        menuString = "Login";
    }

    @Override
    public String getMenuString() {
        return menuString;
    }

    private static Login currentUser;

    Map<Login,UserInformation> listOfUsers = new HashMap<Login, UserInformation>() {{
        put(new Login("111-2222","achu"),new UserInformation("aswathi","achu160@gmail.com","8147716194"));
        put(new Login("222-3333","radha"),new UserInformation("archana","achu170@gmail.com","8147716195"));
        put(new Login("333-4444","lakshmi"),new UserInformation("lakshmi","achu180@gmail.com","8147716196"));
        put(new Login("444-5555","babu"),new UserInformation("moorthy","achu190@gmail.com","8147716197"));

    }};

    public boolean checkValidUser(Login login) {
        if (listOfUsers.containsKey(login)) {
            currentUser = login;
            return true;
        }
        return false;
    }

    public Map<Login, UserInformation> getListOfUsers() {
        return listOfUsers;
    }

    public static Login getCurrentUser() {
        return currentUser;
    }

    public static void logout() {
        currentUser = null;
    }

    public MenuActionResult performMenuAction(UserInterface userInterface){

        Login login;
        MenuActionResult menuActionResult = new MenuActionResult();
        if (currentUser == null) {
            String userName = userInterface.getUserName();
            String passWord = userInterface.getPassword();
            login = new Login(userName, passWord);
            if (checkValidUser(login)) {
                menuActionResult.setMenuActionDetails(true, "Successfully Logged In");
            } else {
                menuActionResult.setMenuActionDetails(false, "Not a valid user!");
            }
        }


        userInterface.displayMenuActionResult(menuActionResult);
        return menuActionResult;
    }

//    public boolean validateLogin() {
//        Login login;
//        if (currentUser == null) {
//            String userName = userInterface.getUserName();
//            String passWord = userInterface.getPassword();
//            login = new Login(userName, passWord);
//            try {
//                if (checkValidUser(login))
//                    return true;
//            } catch ( Exception e) {
//                userInterface.displayResultString("Not a valid user!");
//            }
//        } else {
//            login = currentUser;
//        }
//        return true;
//    }
}
